import request from '@/utils/request'
import type {
  ApiResponse,
  UserListParams,
  UserListResponse,
  CourseListParams,
  CourseListResponse,
  CourseDetail,
  CategoryStatistics,
  WatchCountStatistics
} from '@/types/api'

// 用户登录
export function login(data) {
  return request({
    url: '/user/login',
    method: 'post',
    data: {
      email: data.email,
      password: data.password
    }
  })
}

// 获取用户信息
export function getUserInfo() {
  return request({
    url: '/user/info',
    method: 'get'
  })
}

// 用户管理接口
export const userApi = {
  // 获取用户列表
  getList(params) {
    return request({
      url: '/admin/user/list',
      method: 'get',
      params: {
        page: params.page || 1,
        pageSize: params.pageSize || 2
      }
    })
  },

  // 冻结用户
  freeze(userId) {
    return request({
      url: `/admin/user/freeze/${userId}`,
      method: 'put'
    })
  },

  // 解冻用户
  unfreeze(userId) {
    return request({
      url: `/admin/user/unfreeze/${userId}`,
      method: 'put'
    })
  }
}

// 课程管理接口
export const courseApi = {
  // 获取课程列表
  getList(params) {
    return request({
      url: '/course/list',
      method: 'get',
      params: {
        page: params.page || 1,
        pageSize: params.pageSize || 10
      }
    })
  },

  // 获取课程详情
  getDetail(courseId) {
    return request({
      url: `/course/${courseId}/chapters`,
      method: 'get'
    })
  },

  // 新增课程
  add(data) {
    const formData = new FormData()
    formData.append('name', data.name)
    formData.append('desc', data.desc)
    formData.append('img', data.img)
    formData.append('price', data.price.toString())

    return request({
      url: '/admin/course',
      method: 'post',
      data: formData,
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  },

  // 修改课程
  update(data) {
    const formData = new FormData()
    formData.append('courseID', data.courseID.toString())
    if (data.name) formData.append('name', data.name)
    if (data.desc) formData.append('desc', data.desc)
    if (data.img) formData.append('img', data.img)
    if (data.price) formData.append('price', data.price.toString())

    return request({
      url: '/admin/course',
      method: 'put',
      data: formData,
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  },

  // 删除课程
  delete(courseId) {
    return request({
      url: '/admin/course',
      method: 'delete',
      data: { courseID: courseId }
    })
  }
}

// 章节管理接口
export const chapterApi = {
  // 添加章节
  add(data) {
    return request({
      url: '/admin/course/chapter',
      method: 'post',
      data: {
        courseID: data.courseID,
        title: data.title,
        order: data.order
      }
    })
  },

  // 添加课程资料
  addMaterial(data) {
    const formData = new FormData()
    formData.append('chapterID', data.chapterID.toString())
    formData.append('type', data.type)
    formData.append('filePath', data.filePath)

    return request({
      url: '/admin/chapter/material/put',
      method: 'post',
      data: formData,
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  },

  // 更新课程资料
  updateMaterial(data) {
    const formData = new FormData()
    formData.append('materialID', data.materialID.toString())
    formData.append('chapterID', data.chapterID.toString())
    formData.append('type', data.type)
    formData.append('filePath', data.filePath)

    return request({
      url: '/admin/chapter/material/update',
      method: 'post',
      data: formData,
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  },

  // 删除课程资料
  deleteMaterial(materialId) {
    return request({
      url: '/admin/chapter/material/delete',
      method: 'post',
      data: { materialID: materialId }
    })
  }
}

// 统计分析接口
export const statisticsApi = {
  // 获取课程分类统计
  getCategoryStatistics(params) {
    return request({
      url: '/admin/course/statistics',
      method: 'get',
      params: {
        page: params.page || 1,
        pageSize: params.pageSize || 5
      }
    })
  },

  // 获取观看次数统计
  getWatchCount(params) {
    return request({
      url: '/admin/course/watch-count',
      method: 'get',
      params: {
        page: params.page || 1,
        pageSize: params.pageSize || 5
      }
    })
  }
} 